package com.ruoyi.web.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.service.ISysDeptService;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.api.process.model.ProcessInstance;
import org.activiti.api.process.model.builders.ProcessPayloadBuilder;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import com.ruoyi.system.service.ISysUserService;

import com.ruoyi.web.domain.ArBorrowDetail;
import com.ruoyi.web.domain.ArBorrowInfo;
import com.ruoyi.web.mapper.ArBorrowDetailMapper;
import com.ruoyi.web.mapper.ArBorrowInfoMapper;
import org.activiti.api.process.model.ProcessInstance;
import org.activiti.api.process.model.builders.ProcessPayloadBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.service.IArBorrowInfoService;

/**
 * 借阅管理Service业务层处理
 *
 * @author ruoyi_ln
 * @date 2022-02-15
 */
@Service
public class ArBorrowInfoServiceImpl implements IArBorrowInfoService
{
    @Autowired
    private ArBorrowInfoMapper arBorrowInfoMapper;
    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ArBorrowDetailMapper arBorrowDetailMapper;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ProcessRuntime processRuntime;

    /**
     * 查询借阅管理
     *
     * @param recNo 借阅管理ID
     * @return 借阅管理
     */
    @Override
    public ArBorrowInfo selectArBorrowInfoById(String recNo)
    {
        return arBorrowInfoMapper.selectArBorrowInfoById(recNo);
    }

    /**
     * 查询借阅管理列表
     *
     * @param arBorrowInfo 借阅管理
     * @return 借阅管理
     */
    @Override
    public List<ArBorrowInfo> selectArBorrowInfoList(ArBorrowInfo arBorrowInfo)
    {
        List<ArBorrowInfo> arBorrowInfos = arBorrowInfoMapper.selectArBorrowInfoList(arBorrowInfo);
        for (ArBorrowInfo borrowInfo : arBorrowInfos) {
            List<ArBorrowDetail> arBorrowDetails = arBorrowDetailMapper.selectArBorrowDetailByInfoId(borrowInfo.getRecNo());
            borrowInfo.setArBorrowDetailList(arBorrowDetails);
        }
        return arBorrowInfos;
    }

    /**
     * 新增借阅管理
     *
     * @param arBorrowInfo 借阅管理
     * @return 结果
     */
    @Override
    public AjaxResult insertArBorrowInfo(ArBorrowInfo arBorrowInfo)
    {
        SysUser sysUser = userService.selectUserByUserName(SecurityUtils.getUsername());
        SysDept sysDept = deptService.selectDeptById(Long.parseLong(sysUser.getDeptId()));
        arBorrowInfo.setCreateBy(sysDept.getDeptName());
        arBorrowInfo.setCreateTime(DateUtils.getNowDate());
        arBorrowInfo.setUserNo(sysUser.getDeptId());
        String id=UUID.randomUUIDPlus();
        arBorrowInfo.setRecNo(id);
        arBorrowInfo.setBorrowFlag("0");
        arBorrowInfo.setDelFlag("0");
        arBorrowInfoMapper.insertArBorrowInfo(arBorrowInfo);
        // 新增借阅详情
        List<ArBorrowDetail> arBorrowDetailList = arBorrowInfo.getArBorrowDetailList();
        for (ArBorrowDetail arBorrowDetail : arBorrowDetailList) {
            String uuid = UUID.randomUUIDPlus();
            arBorrowDetail.setRecNo(uuid);
            arBorrowDetail.setBorrowInfoNo(id);
            arBorrowDetail.setApproveFlag("0");
            arBorrowDetail.setDelFlag("0");
            arBorrowDetail.setCreateBy(sysDept.getDeptName());
            arBorrowDetail.setCreateTime(DateUtils.getNowDate());
            arBorrowDetailMapper.insertArBorrowDetail(arBorrowDetail);
        }
        return AjaxResult.success();
    }

    /**
     * 修改借阅管理
     *
     * @param arBorrowInfo 借阅管理
     * @return 结果
     */
    @Override
    public int updateArBorrowInfo(ArBorrowInfo arBorrowInfo)
    {
        SysUser sysUser = userService.selectUserByUserName(SecurityUtils.getUsername());
        SysDept sysDept = deptService.selectDeptById(Long.parseLong(sysUser.getDeptId()));
        arBorrowInfo.setUpdateBy(sysDept.getDeptName());
        arBorrowInfo.setUpdateTime(DateUtils.getNowDate());

        // 修改借阅详情
        List<ArBorrowDetail> arBorrowDetailList = arBorrowInfo.getArBorrowDetailList();
        for (ArBorrowDetail arBorrowDetail : arBorrowDetailList) {
            if (arBorrowDetail.getRecNo() == null) {
                String uuid = UUID.randomUUIDPlus();
                arBorrowDetail.setRecNo(uuid);
                arBorrowDetail.setBorrowInfoNo(arBorrowInfo.getRecNo());
                arBorrowDetail.setApproveFlag("0");
                arBorrowDetail.setDelFlag("0");
                arBorrowDetail.setCreateBy(sysDept.getDeptName());
                arBorrowDetail.setCreateTime(DateUtils.getNowDate());
                arBorrowDetailMapper.insertArBorrowDetail(arBorrowDetail);
            }else {
                arBorrowDetail.setUpdateBy(sysDept.getDeptName());
                arBorrowDetail.setUpdateTime(DateUtils.getNowDate());
                arBorrowDetailMapper.updateArBorrowDetail(arBorrowDetail);
            }
        }
        return arBorrowInfoMapper.updateArBorrowInfo(arBorrowInfo);
    }

    /**
     * 批量删除借阅管理
     *
     * @param recNos 需要删除的借阅管理ID
     * @return 结果
     */
    @Override
    public int deleteArBorrowInfoByIds(String[] recNos)
    {
        int i=0;
        for (String recNo : recNos) {
            ArBorrowInfo arBorrowInfo = new ArBorrowInfo();
            arBorrowInfo.setRecNo(recNo);
            SysUser sysUser = userService.selectUserByUserName(SecurityUtils.getUsername());
            SysDept sysDept = deptService.selectDeptById(Long.parseLong(sysUser.getDeptId()));
            arBorrowInfo.setUpdateBy(sysDept.getDeptName());
            arBorrowInfo.setUpdateTime(DateUtils.getNowDate());
            arBorrowDetailMapper.deleteArBorrowDetailByInfoId(arBorrowInfo.getRecNo());
            arBorrowInfoMapper.deleteArBorrowInfoById(arBorrowInfo);
            i++;
        }
        return i;
    }

    /**
     * 删除借阅管理信息
     *
     * @param recNo 借阅管理ID
     * @return 结果
     */
    @Override
    public int deleteArBorrowInfoById(String recNo)
    {

        ArBorrowInfo arBorrowInfo = new ArBorrowInfo();
        arBorrowInfo.setRecNo(recNo);
        SysUser sysUser = userService.selectUserByUserName(SecurityUtils.getUsername());
        SysDept sysDept = deptService.selectDeptById(Long.parseLong(sysUser.getDeptId()));
        arBorrowInfo.setUpdateBy(sysDept.getDeptName());
        arBorrowInfo.setUpdateTime(DateUtils.getNowDate());
        arBorrowDetailMapper.deleteArBorrowDetailByInfoId(arBorrowInfo.getRecNo());
        return arBorrowInfoMapper.deleteArBorrowInfoById(arBorrowInfo);
    }
}
